package vn.com.pavibook.admin.controller;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import vn.com.pavibook.admin.service.ManagePublisherService;
import vn.com.pavibook.admin.service.PersonalInfoService;
import vn.com.pavibook.domain.model.Account;
import vn.com.pavibook.domain.model.PagingInfo;
import vn.com.pavibook.domain.model.Publisher;
import vn.com.pavibook.domain.model.SearchResult;
import vn.com.pavibook.model.vo.PublisherVO;

@Controller
public class ManagePublisherController {

	private static final int offset = 10;
	
	@Autowired
	private ManagePublisherService managePublisherService;
	
	@Autowired
	private PersonalInfoService personalInfoService;
	
	@RequestMapping(value = "/admin/publisher-info", method = RequestMethod.GET)
	public String openManagePublisher(Model model) {
		PagingInfo pagingInfo = new PagingInfo(offset, 1);
		SearchResult<Publisher> result = managePublisherService.getPublisher(pagingInfo, null);
		model.addAttribute("listPublisher", result.getSearchResult());
		model.addAttribute("pagingInfo", result.getPagingInfo());
		return "admin/publisher-info";
	}
	
	@RequestMapping(value = "/admin/searchPublisher", method = RequestMethod.POST)
	public String searchPublisher(Model model, 
			@RequestParam("pageIndex") int pageIndex, @RequestParam("keyword") String keyword) {
		PagingInfo pagingInfo = new PagingInfo(offset, pageIndex);
		SearchResult<Publisher> result = new SearchResult<Publisher>();
		if(StringUtils.isEmpty(keyword)){
			result = managePublisherService.getPublisher(pagingInfo, null);
		} else {
			result = managePublisherService.getPublisher(pagingInfo, keyword);
		}
		model.addAttribute("listPublisher", result.getSearchResult());
		model.addAttribute("pagingInfo", pagingInfo);
		model.addAttribute("keyword", keyword);
		return "admin/publisher-info";
	}
	
	@RequestMapping(value = "/admin/add-publisher", method = RequestMethod.GET)
	public String openAddPublisher(Model model) {
		PublisherVO publisher = new PublisherVO();
		model.addAttribute("publisher", publisher);
		return "admin/add-publisher";
	}

	@RequestMapping(value = "/admin/addPublisher", method = RequestMethod.POST)
	public String addPublisher(Model model,
			@ModelAttribute("publisher") @Valid PublisherVO publisher,
			BindingResult result) {
		if(result.hasErrors()) {
			model.addAttribute("emailExited", false);
            return "admin/add-publisher";
        }
		
		//check username is existed
		if (managePublisherService.checkExistedUsername(publisher.getUsername())) {
			model.addAttribute("emailExited", true);
			return "admin/add-publisher";
		}
		
		managePublisherService.addNewPublisher(publisher);
		model.addAttribute("addStatus", true);
		return "admin/add-publisher";
	}
	
	@RequestMapping(value="admin/ajax/viewDetailPublisher", method=RequestMethod.GET)
	public ModelAndView viewDetailPublisher(@RequestParam("id") int id){
		ModelAndView model = new ModelAndView("admin/view-detail");
		PublisherVO publisherVO = personalInfoService.getPersonalInfoById(id);
		Account account = personalInfoService.getAccountById(publisherVO.getAccountId());
		model.addObject("publisher", publisherVO);
		model.addObject("account", account);
		return model;
	}
	
	@RequestMapping(value="admin/deletePublisher", method=RequestMethod.GET)
	public String deletePublisher(@RequestParam("index") int id, Model model){
		managePublisherService.deletePublisherById(id);
		return "redirect:publisher-info";
	}
}
